package com.baizhi.service.impl;

import com.baizhi.dao.LogDao;
import com.baizhi.entity.Log;
import com.baizhi.service.MqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class MqServiceImpl implements MqService {
    @Autowired(required = false)
    private LogDao logDao;
    @Autowired(required = false)
    private Queue queue;
    @Autowired(required = false)
    private JmsTemplate jmsTemplate;



    @Override
    //这是一个生产者
    public void mqProduct() {
        for(int i=0;i<100;i++){
            jmsTemplate.send(queue, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    Log log=new Log();
                    return session.createTextMessage("cmfz");
                }
            });
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }



    }

//    @Override
//    //这是一个消费者
//    public void mqConsume() {
//        String o = (String) jmsTemplate.receiveAndConvert(queue);
//
//        System.out.println(o);
//    }
}
